<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="flex" :model="formState">
        <a-form-item name="zjm">
          <a-input v-model:value="formState.zjm" class="w-64" placeholder="物品编号/物品名称/助记码" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex justify-start">
        <a-button class="mb-2" type="primary" @click="onAdd"> 新增 </a-button>
      </div>
      <BaseTable fill :data-source="tableData" :loading="loading" :scroll="{ x: 2600 }" column-code="000839" row-key="id" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex == 'mc'">
            <MedicineInfo :value="{ mc: record?.mc, gg: record?.gg, cj: record?.cj }" />
          </template>
          <template v-if="column.dataIndex === 'insureStatus'">
            <a-switch :checked="record.insureStatus" :un-checked-value="0" :checked-value="1" @change="onChangeInsureStatus(record)" />
          </template>
          <template v-if="column.dataIndex === 'subFlag'">
            {{ record.subFlag === 1 ? '是' : '否' }}
          </template>

          <template v-if="column.dataIndex == 'qgpNumber'">
            {{ record.qgpNumber }}
            <span v-if="record.qgpUnit == 0" class="text-gray-400 text-xs">年 </span>
            <span v-if="record.qgpUnit == 1" class="text-gray-400 text-xs">月 </span>
            <span v-if="record.qgpUnit == 2" class="text-gray-400 text-xs">天 </span>
            <span v-if="record.qgpUnit == 3" class="text-gray-400 text-xs">小时 </span>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onEdit(record)">修改</a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="open" :row-data="rowData" @success="getData" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import Edit from './Edit.vue'

const rowData = ref(null)
const formRef = ref(null)
const open = ref(false)
const formState = ref({
  mcbh: ''
})

const { loading, tableData, getData, pagination } = useTable({
  url: '/operation/opex/shanghai/getShmiDictMtrList',
  pagination: true,
  immediate: true,
  params: formState.value
})
const onChangeInsureStatus = async (row) => {
  const status = row.insureStatus === 1 ? 0 : 1
  const { code, msg } = await post('/operation/opex/shanghai/doSaveShmiDictMtr', { id: row.id, bh: row.bh, insureStatus: status })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  getData()
}
const onEdit = (record) => {
  rowData.value = Object.assign({}, record)
  open.value = true
}
const onAdd = () => {
  rowData.value = null
  open.value = true
}
</script>
